﻿/*************************************************************************************
 * 文 件 名:   HK_PointInspector
 * 
 * 描    述:   Point编辑器面板重写
 * 
 * 版    本：  V1.0
 * 创 建 者：  谭潇杰、京产肠饭
 * 创建时间：  2022/1/28
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(HK_Point))]
public class HK_PointInspector : Editor
{
    HK_Point hk_Point;

    SerializedObject serHK_point;
    SerializedProperty lookAtType, lookAtPos, lookAtRot;

    void OnEnable()
    {
        hk_Point = (HK_Point)target;
        serHK_point = new SerializedObject(target);

        lookAtType = serHK_point.FindProperty("lookAtType");
        lookAtPos = serHK_point.FindProperty("lookAtPos");
        lookAtRot = serHK_point.FindProperty("lookAtRot");
    }

    public override void OnInspectorGUI()
    {
        serHK_point.Update();
        EditorGUILayout.PropertyField(lookAtType);

        if (hk_Point.lookAtType == LookAtType.ByQuaternion)
        {
            lookAtPos.vector3Value = new Vector3(hk_Point.transform.position.x, hk_Point.transform.position.y + 1f, hk_Point.transform.position.z);
            EditorGUILayout.PropertyField(lookAtRot);
        }
        else if (hk_Point.lookAtType == LookAtType.ByTarget)
        {
            lookAtRot.quaternionValue = Quaternion.identity;
            EditorGUILayout.PropertyField(lookAtPos);
        }

        serHK_point.ApplyModifiedProperties();

        if (GUI.changed)
        {
            EditorUtility.SetDirty(hk_Point);
        }
    }

    void OnSceneGUI()
    {
        if (hk_Point.lookAtType == LookAtType.Diffuse)
            return;

        if (hk_Point.lookAtType == LookAtType.ByTarget)
        {
            hk_Point.lookAtPos = Handles.PositionHandle(hk_Point.lookAtPos, hk_Point.lookAtRot);
            EditorUtility.SetDirty(hk_Point);
        }

        if (hk_Point.lookAtType == LookAtType.ByQuaternion)
        {
            hk_Point.lookAtRot = Handles.RotationHandle(hk_Point.lookAtRot, hk_Point.lookAtPos);
            EditorUtility.SetDirty(hk_Point);
        }
    }
}
